package com.winz.cultureapp.fragment;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.mine.xrecyclerview.XRecyclerView;
import com.mine.xrecyclerview.rcvadapter.OnItemClickListener;
import com.winz.cultureapp.R;
import com.winz.cultureapp.activity.StadiumListActivity;
import com.winz.cultureapp.adapter.xrcv.StadiumDataAdapter;
import com.winz.cultureapp.base.BaseFragment;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.StadiumListBean;
import com.winz.cultureapp.filter.activity.LocationPopupWindow;
import com.winz.cultureapp.filter.stadium.GnPopupWindow;
import com.winz.cultureapp.filter.stadium.StadiumTypeWindow;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.RequestFactory;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.filterbar.FilterBar;
import com.winz.filterbar.FilterTab;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * 场馆
 */
public class StadiumFragment extends BaseFragment implements XRecyclerView.LoadingListener, OnItemClickListener, FilterBar.SelectedMenuListener, GnPopupWindow.OnFinishListener, LocationPopupWindow.OnFinishListener {
    @BindView(R.id.tv_top_title)
    public TextView mTv_top_title;

    @BindView(R.id.xrcv_stadium)
    public XRecyclerView mXrcv_stadium;
    @BindView(R.id.fl_back)
    FrameLayout mFl_back;
    @BindView(R.id.fb_statium)
    public FilterBar mFb_statium;

    private boolean isRefresh = true;
    private int limit = 10;
    private int page = 1;
    private int totalPage = 0;

    private List<StadiumListBean.DataBean.ListBean> mDatas;
    private StadiumDataAdapter mAdapter;

    private Map<FilterTab,PopupWindow> map_pop = new HashMap<>();

    private LocationPopupWindow mPop_location;
    private StadiumTypeWindow mPop_type;
    private GnPopupWindow mPop_gn;

    private String areaId = "0"; // 区域ID
    private String categoryId = "0"; // 分类ID
    private String functionId = "0"; // 功能ID



    public static StadiumFragment getInstance() {
        StadiumFragment fragment = new StadiumFragment();

        return fragment;
    }

    @Override
    protected int contentView() {
        return R.layout.fragment_stadium;
    }

    @Override
    protected void initViews(View view) {
        mDatas = new ArrayList<>();
        mTv_top_title.setText(getString(R.string.stadium_top_title));
        mFl_back.setVisibility(View.GONE);

        // XrecyclerView
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mXrcv_stadium.setLayoutManager(manager);
        mXrcv_stadium.setPullRefreshEnabled(true);
        mXrcv_stadium.setLoadingMoreEnabled(true);
        mAdapter = new StadiumDataAdapter(mContext, mDatas, R.layout.empty_view, R.layout.item_stadium_list);

        // 初始化
        mPop_location = new LocationPopupWindow(mContext,mFb_statium.getTabs().get(0));
        map_pop.put(mFb_statium.getTabs().get(0),mPop_location);
        mPop_type = new StadiumTypeWindow(mContext,mFb_statium.getTabs().get(1));
        map_pop.put(mFb_statium.getTabs().get(1), mPop_type);
        mPop_gn = new GnPopupWindow(mContext,mFb_statium.getTabs().get(2));
        map_pop.put(mFb_statium.getTabs().get(2), mPop_gn);
    }

    @Override
    protected void initEvents() {

        mXrcv_stadium.setAdapter(mAdapter);
        mXrcv_stadium.setLoadingListener(this);
        mAdapter.setOnItemClickListener(this);




        mFb_statium.setSelectedMenuListener(this);

        /* 区域 */
        mPop_location.setOnFinishListener(new LocationPopupWindow.OnFinishListener() {
            @Override
            public void onFinish(FilterTab flag, String data) {
                flag.setText(data.split(",")[0]);
                map_pop.get(flag).dismiss();
                if (data.split(",").length>1){
                    areaId = data.split(",")[1];
                    getList();
                }
            }
        });
        mPop_location.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mPop_location.getFlag().setFilterTabSelected(false,FilterTab.FLAG_OTHER);
            }
        });

        mPop_type.setOnFinishListener(new StadiumTypeWindow.OnFinishListener() {
            @Override
            public void onFinish(FilterTab flag, String data) {
                flag.setText(data.split(",")[0]);
                map_pop.get(flag).dismiss();
                if (data.split(",").length>1){
                    categoryId = data.split(",")[1];
                    getList();
                }
            }
        });
        mPop_type.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mPop_type.getFlag().setFilterTabSelected(false,FilterTab.FLAG_OTHER);
            }
        });
        mPop_gn.setOnFinishListener(new GnPopupWindow.OnFinishListener() {
            @Override
            public void onFinish(FilterTab flag, String data) {
                flag.setText(data.split(",")[0]);
                map_pop.get(flag).dismiss();
                if (data.split(",").length>1){
                    functionId = data.split(",")[1];
                    getList();
                }
            }
        });
        mPop_gn.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mPop_gn.getFlag().setFilterTabSelected(false,FilterTab.FLAG_OTHER);
            }
        });

    }

    @Override
    protected void loadDatas() {
        getList();
    }

    /**
     * 获取数据列表
     */
    private void getList() {
        showProgressDialog();
        HttpUitls.onGet("unit_search", RequestFactory.getInstance().unit_search(page+"",limit+"",areaId,categoryId,functionId),new OnResponeListener<StadiumListBean>() {
            @Override
            public void onSuccess(StadiumListBean stadiumListBean) {
                Logger.d("unit_search==>>", JsonUtil.getJsonFromObj(stadiumListBean));
                if (stadiumListBean.code == 0){
                            totalPage = stadiumListBean.totalPage;
                    if (stadiumListBean.data.list != null && stadiumListBean.data.list.size()>0){
                        mXrcv_stadium.setLoadingMoreEnabled(true);
                        if (isRefresh){
                            mDatas.clear();
                            mDatas = stadiumListBean.data.list;
                        }else {
                            mDatas.addAll(stadiumListBean.data.list);
                        }
                    }else {
                        mDatas.clear();
                        StadiumListBean.DataBean.ListBean empty = new StadiumListBean.DataBean.ListBean();
                        empty.layout_type = -1;
                        mDatas.add(empty);
                        mXrcv_stadium.setLoadingMoreEnabled(false);
                    }
                    mAdapter.setDatas(mDatas);
                }else {
                    showMsg(stadiumListBean.msg);
                }
                if (isRefresh){
                    mXrcv_stadium.refreshComplete();
                }else {
                    mXrcv_stadium.loadMoreComplete();
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                dismissProgressDialog();
                Logger.d("unit_search==>>", error);
                if (isRefresh){
                    mXrcv_stadium.refreshComplete();
                }else {
                    mXrcv_stadium.loadMoreComplete();
                }
            }
        });

    }

    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        loadDatas();
    }

    /**
     * 加载
     */
    @Override
    public void onLoadMore() {
        isRefresh = false;
        if (page >= totalPage) {
            mXrcv_stadium.setNoMore(true, true, "没有更多数据了");
        } else {
            page++;
            loadDatas();
        }
    }

    @Override
    public void onItemClick(ViewGroup parent, View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putString(Constants.KEY.STADIUM.ITEM_ID,mDatas.get(position).unitId);
        bundle.putString(Constants.KEY.STADIUM.ITEM_TITLE,mDatas.get(position).unitName);
        startActivity(StadiumListActivity.class, false,bundle);
    }



    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, int position) {
        return false;
    }

    /**
     * 选择筛选项
     */
    @Override
    public void onSelectedMenu(FilterTab view, boolean selected) {
        // 打开或者关闭筛选弹窗
        if (selected){
            map_pop.get(view).showAsDropDown(view);
        }else {
            map_pop.get(view).dismiss();
        }

    }

    /**
     * 选择完成
     * @param flag
     * @param data
     */
    @Override
    public void onFinish(FilterTab flag, String data) {
        flag.setText(data);
        map_pop.get(flag).dismiss();
        showMsg(data);
    }
}
